const tool = require('../tools/tool')
const user = require('../methods/user')
const db = require('../db')

module.exports = {
    user: {
        search: {
            exp: '查找用户',
            method: 'post',
            auth: {
                admin: true,
            },
            async func(data) {
                return await user.admin.getList(
                    data.param.page,
                    data.param.size,
                    data.param.account,
                    data.param.username,
                    data.param.phone,
                    data.param.role,
                    data.param.isAdmin,
                    data.param.departmentId,
                    data.param.isDisabled
                )
            }
        },
        single: {
            exp: '获取单个用户详细信息',
            method: 'post',
            auth: {
                admin: true
            },
            async func(data) {
                let re = await user.getSingleUserInfoByUserId(data.param.id, true, false)
                if (!re) throw '用户不存在'
                return re
            }
        },
        saveInfo: {
            exp: '保存用户信息',
            method: 'post',
            auth: {
                admin: true,
            },
            async func(data) {
                if (Object.keys(data.param.values)[0] == 'account') {
                    await user.update.account(data.param.id, data.param.values.account)
                    return
                }
                if (Object.keys(data.param.values)[0] == 'phone') {
                    await user.update.phone_noVCode(data.param.id, data.param.values.phone)
                    return
                }
                if (Object.keys(data.param.values)[0] == 'isAdmin') {
                    let re = await user.getSingleUserInfoByUserId(data.currentUserId)
                    if (!re.isSAdmin) throw '超级管理员才能修改此设置'
                    await user.update.admin(data.param.id, data.param.values.isAdmin)
                    return
                }
                await user.admin.update(data.param.id, data.param.values)
            }
        },
        deleteList: {
            exp: '删除多个用户',
            method: 'post',
            auth: {
                admin: true,
            },
            async func(data) {
                await user.admin.deleteList(data.param.list)
            }
        },
        getListByCampusCategory: {
            exp: '根据校区类别获取用户列表',
            method: 'post',
            auth: {
                admin: true
            },
            async func(data) {
                return await user.admin.getListByCampusCategory(data.param.campusId, data.param.categoryId)
            }
        },

    },
    notification: {
        send: {
            exp: '发送通知',
            method: 'post',
            auth: {
                admin: true
            },
            async func(data) {
                let currentUserData = await tool.methods.user().getSingleUserInfoByUserId(data.currentUserId)
                const handleUserIds = async (ids = []) => {
                    for (let i = 0; i < ids.length; i++) {
                        let cId = ids[i]
                        await tool.methods.notification().send.toUser.byUserId(cId, data.param.content, currentUserData.username)
                        if (data.param.sendMessage) {
                            await tool.methods.sendMessage().toUser.byUserId(cId, data.param.content)
                        }
                    }
                }
                if (data.param.type == 'toAll') {
                    let userIds = []
                    userIds = await (async () => {
                        let re = await db.select(`select id from users`)
                        for (let i = 0; i < re.length; i++)re[i] = re[i].id
                        return re
                    })()
                    await handleUserIds(userIds)
                }
                if (data.param.type == 'toUsers') {
                    let userIds = []
                    userIds = data.param.userIds
                    await handleUserIds(userIds)
                }
                if (data.param.type == 'toHandleGroup') {
                    await tool.methods.notification().send.toHandleGroup(data.param.campusId, data.param.categoryId, data.param.content, currentUserData.username)
                    if (data.param.sendMessage) {
                        await tool.methods.sendMessage().toHandleGroup(data.param.campusId, data.param.categoryId, currentUserData.username)
                    }
                }
                if (data.param.type == 'toReviewGroup') {
                    await tool.methods.notification().send.toReviewGroup(data.param.campusId, data.param.categoryId, data.param.content, currentUserData.username)
                    if (data.param.sendMessage) {
                        await tool.methods.sendMessage().toReviewGroup(data.param.campusId, data.param.categoryId, currentUserData.username)
                    }
                }
                if (data.param.type == 'toDepartment') {
                    await tool.methods.notification().send.toDepartment(data.param.departmentId, data.param.content, currentUserData.username)
                    if (data.param.sendMessage) {
                        await tool.methods.sendMessage().toDepartment(data.param.departmentId, data.param.content)
                    }
                }
                if (data.param.type == 'toRole') {
                    let userIds = []
                    userIds = await (async () => {
                        let re = await db.select(`select id from users where role = '${data.param.role}'`)
                        for (let i = 0; i < re.length; i++)re[i] = re[i].id
                        return re
                    })()
                    await handleUserIds(userIds)
                }
                await tool.methods.notification().send.toUser.byUserId(currentUserData.id, `通知"${data.param.content}"发送成功`, '系统', JSON.stringify({
                    name: 'notifUserList',
                    data: data.param.content
                }))
            }
        },
        checkList: {
            exp: '获取被通知人列表',
            method: 'get',
            auth: {
                admin: true
            },
            async func(data) {
                if (!data.query.content) throw '请输入内容'
                let re = await tool.dbf().selectWithParams(`select 用户ID,用户名,是否已读,阅读时间 from user_notifications_view where 通知内容 = ? order by 是否已读`, [data.query.content])
                for (let i = 0; i < re.length; i++)re[i].userData = await tool.methods.user().getSingleUserInfoByUserId(re[i]['用户ID'])
                return re
            }
        }
    }
}